package com.gitee.huanminabc.db_freedom.segments;


import com.gitee.huanminabc.db_freedom.base.SqlFun;

/**
 * 条件实现类
 * @param <T>
 * @param <Children>
 */
public abstract class SqlConditionImplSegments <T, Children extends SqlSpecialImplSegments<T, Children>>    extends SqlSpecialImplSegments<T, Children> implements SqlConditionSegments<Children, T, SqlFun<T, ?>> {

    public SqlConditionImplSegments() {
    }
    public SqlConditionImplSegments(Class<?> entityClass, com.gitee.huanminabc.db_freedom.wrapper.SQL sql) {
        super(entityClass, sql);
    }
    @Override
    public Children EQ() {
        SQL().append(" = ");
        return typedThis;
    }
    //eq的同时加参
    public Children EQ(Object param){
        SQL().append(EQ()).append(V(param));
        return typedThis;
    }

    @Override
    public Children NE() {
        SQL().append(" != ");
        return typedThis;
    }
    public Children NE(Object param) {
        SQL().append(NE()).append(V(param));
        return typedThis;
    }


    @Override
    public Children GT() {
        SQL().append(" > ");
        return typedThis;
    }
    public Children GT(Object param) {
        SQL().append(GT()).append(V(param));
        return typedThis;
    }

    @Override
    public Children GE() {
        SQL().append(" >= ");
        return typedThis;
    }

    public Children GE(Object param) {
        SQL().append(GE()).append(V(param));
        return typedThis;
    }

    @Override
    public Children LT() {
        SQL().append(" < ");
        return typedThis;
    }
    public Children LT(Object param) {
        SQL().append(LT()).append(V(param));
        return typedThis;
    }

    @Override
    public Children LE() {
        SQL().append(" <= ");
        return typedThis;
    }
    public Children LE(Object param) {
        SQL().append(LE()).append(V(param));
        return typedThis;
    }
    @Override
    public Children BETWEEN() {
        SQL().append(" BETWEEN ");
        return typedThis;
    }

    @Override
    public Children NOT_BETWEEN() {
        SQL().append(" NOT BETWEEN ");
        return typedThis;
    }

    @Override
    public Children LIKE() {
        SQL().append(" LIKE ");
        return typedThis;
    }


    public Children LIKE(Object param) {
        //必须是String类型
        if(!(param instanceof String)){
            throw new RuntimeException("LIKE方法只能接受String类型");
        }
        SQL().append(LIKE()).append(V("%".concat(param.toString()).concat("%")));
        return typedThis;
    }
    public Children LIKE_LEFT(Object param) {
        //必须是String类型
        if(!(param instanceof String)){
            throw new RuntimeException("LIKE_LEFT方法只能接受String类型");
        }
        SQL().append(LIKE()).append(V("%".concat(param.toString())));
        return typedThis;
    }
    public Children LIKE_RIGHT(Object param) {
        //必须是String类型
        if(!(param instanceof String)){
            throw new RuntimeException("LIKE_RIGHT方法只能接受String类型");
        }
        SQL().append(" LIKE ? ");
        SQL().addParam(param.toString().concat("%"));
        return typedThis;
    }

    @Override
    public Children IN() {
        SQL().append(" IN ");
        return typedThis;
    }

    public Children IN(Object... params) {
        //不能传空
        if(params==null||params.length==0){
            throw new RuntimeException("IN方法参数不能为空");
        }
        SQL().append(" IN ").append(LEFT_BRACKET());
        for (Object param : params) {
            SQL().append("?,");
            SQL().addParam(param);
        }
        SQL().deleteCharAt(SQL().length()-1);
        SQL().append(RIGHT_BRACKET());
        return typedThis;
    }

    @Override
    public Children NOT_IN() {
        SQL().append(" NOT IN ");
        return typedThis;
    }

    @Override
    public Children IS_NULL() {
        SQL().append(" IS Null ");
        return typedThis;
    }


    @Override
    public Children IS_NOT_NULL() {
        SQL().append(" IS NOT Null ");
        return typedThis;
    }

}
